package cc.mrbird.febs.business.service.impl;

import cc.mrbird.febs.business.entity.Business;
import cc.mrbird.febs.business.service.IBusinessService;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.business.entity.BusinessModule;
import cc.mrbird.febs.business.mapper.BusinessModuleMapper;
import cc.mrbird.febs.business.service.IBusinessModuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class BusinessModuleServiceImpl extends ServiceImpl<BusinessModuleMapper, BusinessModule> implements IBusinessModuleService {

    @Resource
    private IBusinessService businessService;

    @Override
    public IPage<BusinessModule> query(QueryRequest request, BusinessModule businessModule) {
        LambdaQueryWrapper<BusinessModule> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(businessModule) && StringUtils.isNotBlank(businessModule.getModuleName())) {
            queryWrapper.like(BusinessModule::getModuleName, businessModule.getModuleName());
        }
        queryWrapper.orderByDesc(BusinessModule::getId);
        Page<BusinessModule> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public Boolean add(BusinessModule businessModule) {
        LambdaQueryWrapper<BusinessModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessModule::getModuleName, businessModule.getModuleName());
        int res = this.baseMapper.selectCount(queryWrapper);
        if (res > 0) {
            return false;
        } else {
            businessModule.setCreateBy(FebsUtil.getCurrentUser().getUsername());
            businessModule.setCreateTime(new Date());
            this.save(businessModule);
            return true;
        }
    }

    @Override
    public Boolean update(BusinessModule businessModule) {
        LambdaQueryWrapper<BusinessModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessModule::getModuleName, businessModule.getModuleName());
        queryWrapper.ne(BusinessModule::getId, businessModule.getId());
        int res = this.baseMapper.selectCount(queryWrapper);
        if (res > 0) {
            return false;
        } else {
            businessModule.setModifyBy(FebsUtil.getCurrentUser().getUsername());
            businessModule.setModifyTime(new Date());
            this.saveOrUpdate(businessModule);
            return true;
        }
    }

    @Override
    public Boolean remove(String ids) {
        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Business::getModuleId, ids);
        int res = businessService.getBaseMapper().selectCount(queryWrapper);
        if (res > 0) {
            return false;
        } else {
            this.removeByIds(Arrays.asList(ids.split(",")));
            return true;
        }
    }

    @Override
    public List<BusinessModule> listAll() {
        LambdaQueryWrapper<BusinessModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(BusinessModule::getId);
        return this.getBaseMapper().selectList(queryWrapper);
    }
}
